package edu.uta.BillMeApp;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;

import edu.BillME.datapars.HttpConnection;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/* @auther Siddharth Patel & Susan Foster */
public class PaymentConfirmation extends Activity{
	
	final static String URL="http://omega.uta.edu/~smp3753/email.php"; 
	String getWN;
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paymentdetails);
        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        TextView result;
        JSONArray jArray;
        String email1 = null;
    
        result=(TextView)findViewById(R.id.conftext);
        Button confrm = (Button) findViewById(R.id.conf);
        Button settle = (Button) findViewById(R.id.settle);
        
        Bundle gotBundle=getIntent().getExtras();
        final String friendName = gotBundle.getString("fN");
        getWN=gotBundle.getString("wN");
        
        final String[]split= friendName.split("\\s+");  
		Log.e("at pay split[0]", split[0]);
		Log.e("at paysplit[1]", split[1]);
		Log.e("at paysplit[2]", split[2]);
		Log.e("at pay split[3]", split[3]);
		Log.e("at pay split[4]", split[4]);
		Log.e("at paysplit[5]", split[5]);
		Log.e("at pay split[6]", split[6]);
		Log.e("at paysplit[7]", split[7]);
		
		String obj = ("Username :"+split[0]+"\n Friends Name :"+split[5]+"\nAmount :"+split[3]+"\n"+split[6]+" "+split[7]+"\n");
		result.setText(obj); 
		Log.e("at pay !!!split[7]", obj);
		result.setTextColor(Color.WHITE);

		 postParameters.add(new BasicNameValuePair("firstname", split[0]));
        //paring data
        try{
        	 jArray = new JSONArray(HttpConnection.executeHttpPost(URL,postParameters));
             Log.d("connection ","connection at TOC");
             Log.d("connection ",jArray.toString());
             for(Integer i=0; i< jArray.length(); i++){
                 try{
                     //Get My JSONObject and grab the String Value that I want.
                     email1 = jArray.getJSONObject(i).getString("emailId");
                     Log.d("email varefication",email1);
                     
                                
                     
                     
                 }catch(JSONException e){
                  Toast.makeText(getBaseContext(), "email address not recovered" ,Toast.LENGTH_LONG).show();
                 }
                                  
       
             }

         }catch (Exception e1) {
          Log.e("log_tag", "Error final1212 "+e1.toString());
          }
		
		confrm.setOnClickListener(new View.OnClickListener(){

			//@Override
		
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			
				postParameters.add(new BasicNameValuePair("billid", split[7]));

				String response = null;
				if(split[5].equals(getWN)){
				try {
					response = HttpConnection.executeHttpPost("http://omega.uta.edu/~smp3753/confirmationbill.php", postParameters);
				
					String res=response.toString();
					res= res.replace("\n","");
					if(res.equals("1")){
						Toast.makeText(getApplicationContext(), "Bill confirmed" ,Toast.LENGTH_LONG).show();
					}
					else{
						Toast.makeText(getApplicationContext(), "Error in connection" ,Toast.LENGTH_LONG).show();
					}

				}catch (Exception e) {
					
				}
			}
				else{
					Toast.makeText(getApplicationContext(), "You cannot confirm this" ,Toast.LENGTH_LONG).show();
				}
				
								
			}
		});		
		
		final String emailid= email1;
		final String msg= obj;
        Log.d("emailId varification",emailid);
        Log.d("msg varefication",msg);
		
		settle.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				
				

			Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
			String[] recipients = new String[]{emailid, "",};
			emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
			emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Billme Reminder");
			emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,msg );
			emailIntent.setType("text/plain");
			startActivity(Intent.createChooser(emailIntent, "Send mail..."));
			finish();
			}

			});
		
	}
}